#include "customizedialog.h"
#include "ui_customizedialog.h"
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>

CustomizeDialog::CustomizeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CustomizeDialog)
{
    ui->setupUi(this);
    InitUi();
}

CustomizeDialog::~CustomizeDialog()
{
    delete ui;
}

void CustomizeDialog::InitUi()
{
    connect(ui->file_load,&QPushButton::clicked,this,&CustomizeDialog::LoadFile_Button_Click);
    connect(ui->file_enter,&QPushButton::clicked,this,&CustomizeDialog::Enter_Button_Click);
    connect(ui->file_cancle,&QPushButton::clicked,this,&CustomizeDialog::Cancle_Button_Click);
}

void CustomizeDialog::Enter_Button_Click()
{
    QString name=ui->libname_line->text();
    QString text=ui->words_edit->toPlainText();
    if(name.isEmpty())
    {
        QMessageBox::warning(this,"warning","词库名不能为空");
        return;
    }else if(text.isEmpty())
    {
        QMessageBox::warning(this,"warning","关键词内容为空");
        return;
    }
    _pairwords.first=name;
    _pairwords.second=text.split('\n');
    accept();
}

void CustomizeDialog::Cancle_Button_Click()
{
    reject();
}

void CustomizeDialog::LoadFile_Button_Click()
{
    QString path=QFileDialog::getOpenFileName(this,"open",QDir::currentPath(),"KeyWords (*.kw)");
    if(path.isEmpty())
        return;
    QFile fp(path);
    if(!fp.open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(this,"warning","文件打开错误");
        return;
    }
    ui->words_edit->clear();
    QString text=QString(fp.readAll());
    ui->words_edit->appendPlainText(text);
}

int CustomizeDialog::exec()
{
    ui->words_edit->clear();
    ui->libname_line->clear();
    return QDialog::exec();
}

const QPair<QString,QStringList>& CustomizeDialog::GetWordLibInfor()
{
    return this->_pairwords;
}
